layui.use(['table','common'], function(){
    var table = layui.table;
    var common=layui.common;
    table.render({
         elem: '#data'
        ,url:'/business/busiReport/reportList'
        ,cols: [[
            {type:'checkbox'},
            {field:'reportId', width:80, title: 'ID', sort: true}
            ,{field:'reportName', width:80, title: '名称'}
            ,{field:'reportDesc', minWidth: 100, title: '描述'}
            ,{field:'reportSql', title: '查询SQL', minWidth: 100}
            ,{field:'createTime', minWidth:80, title: '创建时间', sort: true,
                templet: function(d){
                   return common.dateFormat(new Date(d.createTime),'yyyy-MM-dd hh:mm:ss');
                }
             }
            ,{fixed: 'right', title:'操作', width:150, align:'center', toolbar: '#barOperate'}
        ]]
        ,page: true
    });
    //监听表格复选框选择
    table.on('checkbox(user)', function(obj){
        console.log(obj)
    });
    table.on('tool(user)', function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'detail'){ //查看
            //do somehing
        } else if(layEvent === 'del'){ //删除
            layer.confirm('真的删除行么', function(index){
                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                layer.close(index);
                //向服务端发送删除指令
            });
        } else if(layEvent === 'edit'){ //编辑
            //do something

            //同步更新缓存对应的值
            obj.update({
                username: '123'
                ,title: 'xxx'
            });
        }
    });
    var $ = layui.$, active = {
        getCheckData: function(){ //获取选中数据
            var checkStatus = table.checkStatus('data')
                ,data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
        ,getCheckLength: function(){ //获取选中数目
            var checkStatus = table.checkStatus('data')
                ,data = checkStatus.data;
            layer.msg('选中了：'+ data.length + ' 个');
        }
        ,isAll: function(){ //验证是否全选
            var checkStatus = table.checkStatus('data');
            layer.msg(checkStatus.isAll ? '全选': '未全选')
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});